/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MODEL.DAO;

import MODEL.POJO.TinTucPOJO;
import MODEL.UTIL.MySqlDataAccessHelper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author kijoo
 */
public class TinTucDAO {

    private static ArrayList<TinTucPOJO> LayDanhSachTinTuc(ResultSet rs) throws SQLException {
        ArrayList<TinTucPOJO> ds = new ArrayList<TinTucPOJO>();
        while (rs.next()) {
            TinTucPOJO tt = new TinTucPOJO();
            tt.setIdtintuc(rs.getInt("idtintuc"));
            tt.setIdtin(rs.getString("idtin"));;
            tt.setTen(rs.getString("ten"));
            tt.setHinh(rs.getString("hinh"));
            tt.setTomtat(rs.getString("tomtat"));
            tt.setNoidung(rs.getString("noidung"));
            tt.setGhichu(rs.getString("ghichu"));
            tt.setTinhtrang(rs.getInt("tinhtrang"));
            ds.add(tt);
        }
        return ds;
    }

    public static ArrayList<TinTucPOJO> LayTinMoiXuatHien() {
        ArrayList<TinTucPOJO> ds = new ArrayList<TinTucPOJO>();
        int tinhtrang = 1;
        int batDau = 0;
        String sql = String.format("SELECT * FROM TINTUC WHERE TINHTRANG = %d ORDER BY IDTINTUC DESC", tinhtrang);

        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.executeQuery(sql);
            ds = LayDanhSachTinTuc(rs);
            helper.Close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }

    public static TinTucPOJO LayChiTietTin(String idtin) {
        TinTucPOJO tt = new TinTucPOJO();
        int tinhtrang = 1;
        String sql = String.format("SELECT * FROM TINTUC WHERE IDTIN='%s' AND TINHTRANG='%d'", idtin, tinhtrang);
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                tt.setIdtintuc(rs.getInt("idtintuc"));
                tt.setIdtin(rs.getString("idtin"));
                tt.setTen(rs.getString("ten"));
                tt.setHinh(rs.getString("hinh"));
                tt.setTomtat(rs.getString("tomtat"));
                tt.setNoidung(rs.getString("noidung"));
                tt.setGhichu(rs.getString("ghichu"));

            }
            helper.Close();
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return tt;
    }

    public static boolean ThemTinTuc(String idtin, String ten, String hinh, String tomtat, String noidung, String ghichu) {
        boolean kq = false;
        try {
            int tinhtrang = 1;
            String sql = String.format("INSERT INTO `nama`.`tintuc` (`idtin`, `ten`, `hinh`, `tomtat`, `noidung`, `ghichu`, `tinhtrang`) VALUES ('%s', '%s', 'images/SanPham/%s', '%s', '%s', '%s', %d)", idtin, ten, hinh, tomtat, noidung, ghichu, tinhtrang);
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            int n = helper.executeUpdate(sql);
            if (n == 1) {
                kq = true;
            }
            helper.Close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return kq;

    }

    public static boolean CapNhatTinTuc(String idtin, String ten, String hinh, String tomtat, String noidung, String ghichu) {
        boolean kq = false;
        try {
            String sql = String.format("UPDATE `nama`.`tintuc` SET `ten`='%s', `hinh`='images/SanPham/%s', `tomtat`='%s', `noidung`='%s', `ghichu`='%s' WHERE `idtin`='%s' ", ten, hinh, tomtat, noidung, ghichu, idtin);
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            int n = helper.executeUpdate(sql);
            if (n == 1) {
                kq = true;
            }
            helper.Close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return kq;

    }

    public static boolean XoaTinTuc(String idtin) {
        boolean kq = false;
        try {
            int tinhtrang = 0;
            String sql = String.format("UPDATE `nama`.`tintuc` SET `tinhtrang`=%d WHERE `idtin`='%s'",tinhtrang, idtin);
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.Open();
            int n = helper.executeUpdate(sql);
            if (n == 1) {
                kq = true;
            }
            helper.Close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return kq;

    }
}
